package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.apache.tomcat.util.http.fileupload.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author:zimmor
 * @Date: 2021/2/28 8:53
 * @Description 标签控制层
 * @version: 1.0.0
 */
@RestController
@RequestMapping("/label")
@CrossOrigin
//使用消息总线后，配置中心的配置文件如果是自己定义的配置时而不是第三方的系统配置，发送了post请求后这些值不会改变，加上下面的这个注解就可以刷新配置的值了。
@RefreshScope
public class LabelController {
    @Autowired
    private LabelService labelService;

    @Autowired
    private HttpServletRequest request;

    @Value("${ip}")
    private String ip;

    /**
     * 分页查询
     * @param label
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value="/search/{page}/{size}")
    public Result findSearch(@RequestBody Label label, @PathVariable int page, @PathVariable int size) {
        Page pageData = labelService.findSearch(label, page, size);
        return new Result(true, StatusCode.OK, "分页查询成功", new PageResult<Label>(pageData.getTotalElements(), pageData.getContent()));
    }

    /**
     * 条件查询
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value="/search")
    public Result findSearch(@RequestBody Label label) {
        List<Label> list = labelService.findSearch(label);
        return new Result(true, StatusCode.OK, "条件查询成功", list);
    }

    /**
     * 根据id查询标签
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value="/{id}")
    public Result findById(@PathVariable String id) {
        System.out.println("no3");
        return new Result(true, StatusCode.OK, "根据id查询标签", labelService.findById(id));
    }

    /**
     * 修改标签
     * @param id
     * @param label
     * @return
     */
    @RequestMapping(method=RequestMethod.PUT, value="/{id}")
    public Result update(@PathVariable String id, @RequestBody Label label){
        labelService.update(label);
        return new Result(true, StatusCode.OK, "修改标签");
    }

    /**
     * 删除标签
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE, value="/{id}")
    public Result deleteById(@PathVariable String id) {
        labelService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除标签");
    }

    /**
     * 新增标签
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Label label){
        labelService.add(label);
        return new Result(true, StatusCode.OK,"新增标签");
    }

    /**
     * 查询所有标签
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        String authorization = request.getHeader("Authorization");
        System.out.println(authorization);
        System.out.println(ip);
        return new Result(true, StatusCode.OK, "查询全部标签", labelService.findAll());
    }
}
